[sonic-db-cli] Support --json option#1205
Open
bgallagher-nexthop wants to merge 2 commits into
Open
Conversation
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
2003551 to
f4b989d
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
9 tasks
Contributor
Author
|
#1206 & sonic-net/sonic-buildimage#27836 are published to fix the build failure on this PR (it's a breakage on master). |
There was a problem hiding this comment.
Pull request overview
This PR adds an optional JSON output mode to sonic-db-cli so command results can be consumed by JSON tools (e.g., jq) without changing the existing default output format used by current tooling.
Changes:
- Add
-j/--jsonCLI flag and plumb it through command execution to select JSON formatting. - Implement
swss::RedisReply::to_json_string()to serialize hiredis replies into valid JSON (withHGETALL/HSCANfield-maps as JSON objects). - Add unit tests for JSON output and update the CLI help golden output.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tests/cli_ut.cpp |
Adds coverage validating JSON formatting across several Redis reply types. |
tests/cli_test_data/cli_help_output.txt |
Updates expected sonic-db-cli help text to include --json. |
sonic-db-cli/sonic-db-cli.h |
Extends Options and executeCommands() signature to carry JSON mode. |
sonic-db-cli/sonic-db-cli.cpp |
Implements -j/--json parsing and switches output formatting based on the flag. |
common/redisreply.h |
Declares new RedisReply::to_json_string() API. |
common/redisreply.cpp |
Implements JSON conversion logic using nlohmann JSON. |
Comments suppressed due to low confidence (1)
sonic-db-cli/sonic-db-cli.cpp:238
--namespaceis declared withoptional_argument, so--namespace=asic0will setoptarg, but the current parsing ignoresoptargand instead consumes the next argv token (db_or_op) as the namespace. This breaks a valid getopt_long invocation form.
case 'j':
options.m_json = true;
break;
case 'n':
if (optind < argc)
{
options.m_namespace = argv[optind];
optind++;
}
else
{
throw invalid_argument("namespace value is missing.");
}
break;
Collaborator
|
Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks! ---Powered by SONiC BuildBot
|
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
daecf91 to
2090c48
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1204
Background
sonic-db-clidoes not output valid JSON, because it is designed to output the Pythonrepr()representation of a dictionary. This means it does not work with tools likejqto format the output in a more readable manner.Changes
This PR addresses that problem by introducing a new CLI option to
sonic-db-cli:--jsonor-j. This causessonic-db-clito output valid JSON that is usable with tools likejqwithout breaking any backwards compatibility with users or tooling that relies onsonic-db-cli's current output format.Testing
New unit-tests have been written.
Tested manually on a network switch: